<template>
  <base-breadcrumb :title="$route.meta.title">
    <a-row
      :gutter="10"
      class="page-height"
    >
      <!-- 树搜索 -->
      <a-col
        :span="6"
        class="permission-collapsed"
        :class="{ 'permission-tree-collapsed': collapsed }"
      >
        <a-card
          :style="{ border: collapsed ? 'none' : 'border: 1px solid #e8e8e8' }"
          class="page-height-tree"
        >
          <title-name title="组织架构" />
          <PermissionTreeOrganization
            :collapsed.sync="collapsed"
            @select="handleSelect"
          />
        </a-card>
      </a-col>
      <a-col
        :span="18"
        class="permission-collapsed page-height-list-wrapper"
        :style="collapsed ? 'width:calc(100% - 10px)' : ''"
      >
        <searchData
          :search-data="searchArr"
          @search="search"
        />

        <a-card
          :bordered="false"
          class="page-height-list"
        >
          <!-- <title-name title="项目经理风险底线管理"></title-name> -->

          <div class="table-operator">
            <a-button
              type="primary"
              @click="handleAdd"
              v-if="iSaddShow"
              v-btnPermission="[{code:'seex_pm_031_add_01',ident:'prma_seex'},{code:'buco_pm_031_add_01',ident:'prma_buco'}]"
            >新增</a-button>
          </div>
          <div class="pub-table-select-count">
            <a-icon
              type="info-circle"
              theme="twoTone"
            />
            <span style="margin-right: 12px">
              扣分记录：<span class="totalNum">{{ totalDels.pointsDeducted }} 个</span> | 待提交审核：<span class="totalNum">{{ totalDels.toAudit }} 个</span>
              | 审核中：<span class="totalNum">{{ totalDels.audit }} 个</span> | 审核不通过：<span class="totalNum">{{ totalDels.notApproved }} 个</span>
              | 审核通过：<span class="totalNum">{{ totalDels.passTheAudit }} 个</span>
            </span>
          </div>

          <list-table
            ref="table"
            size="default"
            :rowKey="(record) => record.id"
            :columns="columns"
            :request="loadData"
            :alert="true"
            show-size-changer
            show-quick-jumper
            showPagination="auto"
          >
            <span
              slot="serial"
              slot-scope="text, record, index"
            >{{ index + 1 }}</span>
            <span
              slot="remainingPointsBeforeDeduction"
              slot-scope="text, record, index"
            >{{
              Number(record.remainingPointsBeforeDeduction).toFixed(2)
            }}</span>
            <span
              slot="pointsScore"
              slot-scope="text, record, index"
            >{{ Number(record.pointsScore).toFixed(2) }}</span>
            <span
              slot="createTime"
              slot-scope="text, record, index"
            >
              {{ record.createTime.trim().split(' ')[0] }}
            </span>
            <span
              slot="action"
              slot-scope="text, record"
            >
              <template>
                <MoreButton>
                  <a
                    @click="handleDels(record)"
                    v-btnPermission="[{code:'seex_pm_031_loo_02',ident:'prma_seex'},{code:'buco_pm_031_loo_02',ident:'prma_buco'}]"
                  >查看</a>
                  <span v-if="(record.statusCode == 0 || record.statusCode == 3)&& hideBuquBtn() && $userInfo().employee.userId === record.creatorId">
                    <a-divider
                      type="vertical"
                      v-btnPermission="[{code:'seex_pm_031_edi_03',ident:'prma_seex'},{code:'buco_pm_031_edi_03',ident:'prma_buco'}]"
                    /><a
                      @click="handleEdit(record)"
                      v-btnPermission="[{code:'seex_pm_031_edi_03',ident:'prma_seex'},{code:'buco_pm_031_edi_03',ident:'prma_buco'}]"
                    >编辑</a>
                  </span>
                  <span v-if="record.statusCode == 1 || record.statusCode == 2 || record.statusCode == 3">
                    <a-divider
                      type="vertical"
                      v-btnPermission="[{code:'seex_pm_031_moi_05',ident:'prma_seex'},{code:'buco_pm_031_moi_05',ident:'prma_buco'}]"
                    /><a
                      @click="handleApproveDetail(record)"
                      v-btnPermission="[{code:'seex_pm_031_moi_05',ident:'prma_seex'},{code:'buco_pm_031_moi_05',ident:'prma_buco'}]"
                    >审批信息</a>
                  </span>
                </MoreButton>
              </template>
            </span>
          </list-table>

          <ApprovalDetails
            :title="'审批详情'"
            :businessId="businessId"
            :visible="visibleApprove"
            @cancel="visibleApprove = false"
            @confirm="visibleApprove = false"
          />
        </a-card>
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getPointsRecordList, getPointsRecordTotal } from '@/api/project/pointsRecord'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import ApprovalDetails from '@/components/approvalDetails'
import ARow from 'ant-design-vue/es/grid/Row'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'

const searchArr = [
  // 搜索框
  {
    name: '项目经理名称',
    value: 'projectManager',
    type: 'input'
  },
  // {
  //   name: '项目名称',
  //   value: 'projectName',
  //   type: 'input'
  // },
  // {
  //   name: '分公司',
  //   valueKey: 'branchCompanyId',
  //   type: SelectSubCompany
  // },
  {
    name: '创建人',
    value: 'creatorName',
    type: 'input'
  },
  {
    name: '创建时间阶段',
    value: 'createdTime',
    type: 'dateRange'
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: [
      { label: '全部', value: '' },
      { label: '待提交审核', value: '0' },
      { label: '审核中', value: '1' },
      { label: '审核通过', value: '2' },
      { label: '审核不通过', value: '3' }
    ]
  }
]

const columns = [
  {
    title: '序号',
    width: 65,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '项目经理名称',
    width: 120,
    dataIndex: 'projectManager'
  },
  {
    title: '项目',
    width: 100,
    dataIndex: 'projectName'
  },
  {
    title: '分公司',
    width: 90,
    dataIndex: 'branchCompanyName'
  },
  {
    title: '项目编号',
    width: 100,
    dataIndex: 'projectNum'
  },
  {
    title: '扣分前剩余分数',
    width: 120,
    dataIndex: 'remainingPointsBeforeDeduction',
    scopedSlots: { customRender: 'remainingPointsBeforeDeduction' }
  },
  {
    title: '扣分分数',
    width: 100,
    dataIndex: 'pointsScore',
    scopedSlots: { customRender: 'pointsScore' }
  },
  {
    title: '创建人',
    width: 90,
    dataIndex: 'creatorName'
  },
  {
    title: '创建时间',
    width: 100,
    dataIndex: 'createTime',
    scopedSlots: { customRender: 'createTime' }
  },
  {
    title: '状态',
    width: 80,
    dataIndex: 'statusName'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 220,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'a' + Date.now(),
  components: {
    ApprovalDetails,
    STable,
    PermissionTreeOrganization
  },
  data() {
    return {
      columns,
      searchArr,
      businessId: '',
      visibleApprove: false,

      visible: false,
      title: '',

      visibles: false,
      titles: '',

      form: {},
      // 收缩展开树
      collapsed: false,
      //组织架构选中项
      queryParam: {},
      //组织架构选中项
      select: {},

      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const obj = {}
        // if (!this.select) {
        //   return
        // }
        // 二级-分公司id
        if (this.select?.tageCode === 'BC') {
          obj.branchComId = this.select.orgId
        }
        // 三级-项目id
        if (this.select?.tageCode === 'PD') {
          obj.projectId = this.select.projectId
        }
        if (this.queryParam.createdTime && this.queryParam.createdTime.length > 0) {
          this.queryParam.createTimeStart = this.queryParam.createdTime[0].format('YYYY-MM-DD')
          this.queryParam.createTimeEnd = this.queryParam.createdTime[1].format('YYYY-MM-DD')
          delete this.queryParam.createdTime
        }
        const requestParameters = Object.assign({}, parameter, { ...this.queryParam, ...obj })
        this.getPointsRecordTotal()
        return getPointsRecordList(requestParameters).then((res) => {
          return res
        })
      },

      totalDels: {} // 统计详情
    }
  },

  created() {},
  computed: {
    iSaddShow() {
      let arr = ['safetySupervision','engineeringManage','finance','businessManage','contractLaw','TMQM']
      let code = this.$userInfo()?.currentPost?.deptCode
      return arr.indexOf(code) !== -1;
    }
  },
  methods: {
    /**
     * @description: 树选中
     * @param {object} node 节点对象
     * @return {void}
     */
    handleSelect(node) {
      this.select = node
      this.search()
    },
    // 审批详情
    handleApproveDetail(record) {
      this.visibleApprove = true
      this.businessId = record.id
    },

    // 新增扣分记录
    handleAdd() {
      this.$router.push({ path: '/project/pointsRecord/add' })
    },

    // 搜索
    search(val = {}) {
      this.queryParam = val
      this.$refs.table.refresh(true)
      this.getPointsRecordTotal()
    },

    // 统计
    getPointsRecordTotal() {
      const obj = {}
      // 二级-分公司id
      if (this.select.tageCode === 'BC') {
        obj.branchComId = this.select.orgId
      }
      // 三级-项目id
      if (this.select.tageCode === 'PD') {
        obj.projectId = this.select.projectId
      }
      const requestParameters = Object.assign({}, { ...this.queryParam, ...obj })
      getPointsRecordTotal(requestParameters).then((res) => {
        this.totalDels = res.data
      })
    },

    handleDels(e) {
      // 查看详情
      this.$router.push({ path: '/project/pointsRecord/details', query: { id: e.id } })
    },

    handleEdit(e) {
      // 编辑
      this.$router.push({ path: '/project/pointsRecord/edit', query: { id: e.id } })
    }
  },
  activated() {
    this.getPointsRecordTotal()
  }
}
</script>


